import {
	createStore
} from 'vuex'
import { getInfo } from '@/common/API/login';

export default createStore({
	state: {
		"accessToken": '',
		"expiresTime": '',
		"refreshToken": '',
		"userId": '',
		"userInfo": {},
		"userPhone": "",
		"openId": "",
		"doorPassImg": "",
		"doorPassCard": "",
	},
	mutations: {
		UPDATE_LOGININFO(state, info) {
			state.accessToken = info.accessToken;
			state.expiresTime = info.expiresTime;
			state.refreshToken = info.refreshToken;
			state.userId = info.userId;
		},
		UPDATE_USERINFO(state, info) {
			state.userInfo = info;
			state.userPhone = info.userPhone;
		},
		DOOR_PASS_Img(state, info) {
			state.doorPassImg = info;
		},
		DOOR_PASS_CARD(state, info) {
			state.doorPassCard = info;
		},
		async getUserInfo(state,id){
			let info = await getInfo(id)
			state.userPhone = info.mobile;
			uni.setStorageSync('userPhone', info.mobile);
		}
	},
	actions: {
		setLoginData({
			commit,
			state
		}, loginData) {
			return new Promise(reslove => {
				commit("UPDATE_LOGININFO", loginData);
				uni.setStorageSync('accessToken', loginData.accessToken);
				uni.setStorageSync('expiresTime', loginData.expiresTime);
				uni.setStorageSync('refreshToken', loginData.refreshToken);
				uni.setStorageSync('userId', loginData.userId);
				uni.setStorageSync('userPhone', loginData.userPhone);
				commit("getUserInfo",loginData.userId);
			}).catch(() => {});
		},
		setUserInfo({
			commit,
			state
		}, userInfo) {
			return new Promise(resolve => {
				uni.setStorageSync('userInfo', userInfo)
			})
		},
		SET_OPENID({
			commit,
			state
		}, openId) {
			uni.setStorageSync("openId", openId)
			state.openId = openId;
		},
		logOut({
			commit,
			state
		}) {
			return new Promise(reslove => {
				state.accessToken = ''
				state.userId = ''
				state.userPhone = ''
				let recommendId = uni.getStorageSync('recommendId')
				let alreadyShowPrivacy = uni.getStorageSync('alreadyShowPrivacy')
				uni.clearStorage()
				uni.setStorageSync('recommendId',recommendId)
				uni.setStorageSync('alreadyShowPrivacy',alreadyShowPrivacy)
			}).catch(() => {});
		},
	}
})